package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.IOUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Supplier;
import com.ruoyi.system.service.ISupplierService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SupplyUser;
import com.ruoyi.system.service.ISupplyUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商用户Controller
 * 
 * @author ruoyi
 * @date 2025-08-25
 */
@RestController
@RequestMapping("/system/supplyuser")
public class SupplyUserController extends BaseController
{
    @Autowired
    private ISupplyUserService supplyUserService;
    @Autowired
    private ISupplierService supplierService;

    /**
     * 查询供应商用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(SupplyUser supplyUser)
    {
        startPage();
        List<SupplyUser> list = supplyUserService.selectSupplyUserList(supplyUser);
        return getDataTable(list);
    }

    /**
     * 导出供应商用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @Log(title = "供应商用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupplyUser supplyUser)
    {
        List<SupplyUser> list = supplyUserService.selectSupplyUserList(supplyUser);
        ExcelUtil<SupplyUser> util = new ExcelUtil<SupplyUser>(SupplyUser.class);
        util.exportExcel(response, list, "供应商用户数据");
    }

    /**
     * 获取供应商用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(supplyUserService.selectSupplyUserById(id));
    }

    /**
     * 新增供应商用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "供应商用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SupplyUser supplyUser)
    {
        //处理供货商名称
        if(supplyUser.getSupplyId()!=null && !supplyUser.getSupplyId().equals("") && supplyUser.getSupplyId().longValue()>0 && supplyUser.getSupplyName()==null){
            Supplier supplier = supplierService.selectSupplierById(supplyUser.getSupplyId());
            supplyUser.setSupplyName(supplier.getSuppliName());
        }
        //处理用户密码
        if(StringUtils.isEmpty(supplyUser.getPwd())){
            supplyUser.setPwd(IOUtil.MD5("666666"));
        }
        return supplyUserService.insertSupplyUser(supplyUser);
    }

    /**
     * 修改供应商用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "供应商用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SupplyUser supplyUser)
    {
        //处理供货商名称
        if(supplyUser.getSupplyId()>0){
            Supplier supplier = supplierService.selectSupplierById(supplyUser.getSupplyId());
            supplyUser.setSupplyName(supplier.getSuppliName());
        }
        return toAjax(supplyUserService.updateSupplyUser(supplyUser));
    }

    /**
     * 删除供应商用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "供应商用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(supplyUserService.deleteSupplyUserByIds(ids));
    }
}
